package com.example.springbootthymeleaf.handler;

import com.example.springbootthymeleaf.exception.ApplicationException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
/*
 统一异常处理
 */
@RestControllerAdvice
public class GlobalRestExceptionHandler {

	@ExceptionHandler(NoHandlerFoundException.class)
	public String handleNoHandlerFoundException(NoHandlerFoundException e, Model model) {
		model.addAttribute("msg", e.getMessage());
		return "404";
	}

	// 处理自定义异常ApplicationException
	@ExceptionHandler(ApplicationException.class)
	public String handleApplicationException(ApplicationException e, Model model) {
		model.addAttribute("msg", e.getMessage());
		return "error";
	}

	// 通常在最后的方法处理最大的Exception，保证兜底
	@ExceptionHandler(value = Throwable.class)
	public String handleException(Throwable e, Model model) {
		model.addAttribute("msg", e.getMessage());
		return "error";
	}

}